﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IEC
{
    /// <summary>
    /// AboutWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AboutWindow : Window
    {
        private static readonly string UPDATE_URL = "https://gitee.com/api/v5/repos/frto027/isaac-external-configurator/releases/latest";
        System.Windows.Threading.Dispatcher dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
        public AboutWindow()
        {
            InitializeComponent();
            new System.Threading.Thread(CheckThread).Start();
        }

        public struct LatestReleaseResponse
        {
            public int id;
            public string tag_name, name;
        }

        private void CheckThread(object target)
        {

            try
            {
                var request = WebRequest.Create(UPDATE_URL);
                using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                using (var reader = new System.IO.StreamReader(stream))
                {
                    var json = reader.ReadToEnd();
                    var obj = Newtonsoft.Json.JsonConvert.DeserializeObject< LatestReleaseResponse>(json);
                    var match = System.Text.RegularExpressions.Regex.Match(obj.tag_name, "^v([0-9]+)\\.([0-9]+)$");
                    if (match.Success)
                    {
                        int major = int.Parse(match.Groups[1].Value);
                        int minor = int.Parse(match.Groups[2].Value);
                        dispatcher.BeginInvoke(new Action(() =>
                        {
                            if(!closed)
                            {
                                if (major > MainWindow.VERSION_MAJOR || (major == MainWindow.VERSION_MAJOR && minor > MainWindow.VERSION_MINOR))
                                {
                                    UpdateTb.Visibility = Visibility.Visible;
                                    UpdatePreText.Text = "已有新版本更新（v" + major + "." + minor + "），";
                                }
                                else
                                {
                                    AlreadyNew.Visibility = Visibility.Visible;
                                    AlreadyNew.Text = "当前版本：v" + MainWindow.VERSION_MAJOR + "." + MainWindow.VERSION_MINOR + "，已是最新版本";
                                }
                            }
                        }));
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        private void UpdateBtn_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
            e.Handled = true;
        }

        bool closed = false;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            closed = true;
        }
    }
}
